package com.online.dao.user.Impl;

import com.online.dao.user.UserDao;
import com.online.pojo.UpComing_Shop;
import com.online.pojo.User;
import com.online.pojo.menu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {


    public User getLoginUser(User user,  String name) {
        ResultSet rs;
        try {
            Connection connection = BaseDao.getConnection();
            String sql = "select * from  online.users where user_name=?";
            PreparedStatement pstm = connection.prepareStatement(sql);
            pstm.setString(1, name);
            rs = pstm.executeQuery();
            if (rs.next()) {
                user.setUser_number(rs.getString(1));
                user.setUser_pwd(rs.getString(2));
                user.setUser_name(rs.getString(3));
                user.setUser_permission(rs.getInt(4));
                user.setAccount(rs.getDouble(5));
                System.out.println(user);
            }
            pstm.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }

    public boolean insertUser(String number, String name, String pwd) {
        int rs;
        boolean falg = false;
        try {
            Connection connection = BaseDao.getConnection();
            String sql = "insert into online.users(user_number, user_pwd, user_name, user_permission,account) VALUES (?,?,?,?,?)";
            PreparedStatement pstm = connection.prepareStatement(sql);
            pstm.setString(1, number);
            pstm.setString(2, pwd);
            pstm.setString(3, name);
            pstm.setInt(4, Integer.parseInt("3"));
            pstm.setDouble(5,0);
            rs = pstm.executeUpdate();
            if (rs != 0) {
                falg = true;
                System.out.println("注册成功");
            } else {
                falg = false;
                System.out.println("注册失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return falg;
    }

    public boolean update(String name) throws SQLException {

        boolean falg=false;
        int rs;
        Connection connection = BaseDao.getConnection();
        String sql = "update online.users set user_permission=2 where user_name=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,name);

        rs=pstm.executeUpdate();
        if(rs>0) {
            falg=true;
            System.out.println("更新成功");
        }
        pstm.close();
        connection.close();
        return falg;
    }

    public List<menu> select(List<menu> list) throws SQLException {
        ResultSet rs;
        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.menu";
        PreparedStatement pstm = connection.prepareStatement(sql);
        rs = pstm.executeQuery();
        int i=0;
        while (rs.next()){
            menu menu = new menu();
            menu.setId(rs.getInt(1));
            menu.setName(rs.getString(2));
            menu.setShop_name(rs.getString(3));
            menu.setPrice(rs.getDouble(4));
            menu.setType(rs.getString(5));
            list.add(i,menu);
            i++;
        }
        pstm.close();
        connection.close();
        return list;
    }

    public boolean updateAccount(String name, double account) throws SQLException {
        boolean falg=false;
        int rs;
        Connection connection = BaseDao.getConnection();
        String sql = "update online.users set account=account+? where user_name=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setDouble(1,account);
        pstm.setString(2,name);

        rs=pstm.executeUpdate();
        if(rs>0) {
            falg=true;
            System.out.println("更新成功");
        }
        pstm.close();
        connection.close();
        return falg;
    }

    public boolean updateaccount(String name, double price) throws SQLException {
        boolean falg=false;
        int rs;
        Connection connection = BaseDao.getConnection();
        String sql = "update online.users set account=account-? where user_name=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setDouble(1,price);
        pstm.setString(2,name);

        rs=pstm.executeUpdate();
        if(rs>0) {
            falg=true;
            System.out.println("更新成功");
        }
        pstm.close();
        connection.close();
        return falg;
    }
}